package edu.tms.security;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * 过滤器
 * @author Administrator
 *
 */
public class SecurityFilter implements Filter{
	
	private List<String> loginPaths =new ArrayList<>();//实例化一个装路径的集合
	
	@Override
	public void destroy() {
		// TODO Auto-generated method stub
	}

	@Override
	public void doFilter(ServletRequest arg0, ServletResponse arg1,
			FilterChain chain) throws IOException, ServletException {
		//设置编码
		arg0.setCharacterEncoding("UTF-8"); 
		HttpServletRequest request = (HttpServletRequest) arg0;
        HttpServletResponse response = (HttpServletResponse) arg1;        
        HttpSession session = request.getSession(true);
        //获取用户请求页面
        String requestPath = request.getServletPath();
        //如果发现是css或者js文件，直接放行
        if(requestPath.endsWith(".css") || requestPath.endsWith(".js") 
        		|| requestPath.endsWith(".png")|| requestPath.endsWith(".jpg")){
            chain.doFilter(request, response);
            return;
        }
        //项目路径
        String contextPath =request.getContextPath();
        //若没有登录则进入其他页面则拦截
        if(session == null || session.getAttribute("curUser") == null){
        	//只能进入与登录相关的页面
            if (loginPaths.contains(requestPath)) {
                 chain.doFilter(request, response);
            }else {
                response.sendRedirect(contextPath+"/login.html");
            }
        }else{
//        	System.out.println("requestPath="+requestPath);
        	//与登录相关的都允许进入 下载文件直接放行
        	 if (loginPaths.contains(requestPath) || requestPath.equals("/") || requestPath.contains(".dl")) {
                 chain.doFilter(request, response);
                 return;
            }
        	//所有人都有更改密码的功能与首页
        	if(requestPath.equals("/index.jsp") || requestPath.equals("/s/changepwd.jsp") 
        			|| requestPath.contains("/main/"))
        		chain.doFilter(request, response);
        	else{
        		System.out.println("request:"+requestPath);
        		//获取职位权限
        		String[] pos = (String[]) session.getAttribute("pos");
        		boolean canForward = false;
        		//权限获取失败 重新登录
        		if(pos == null)  response.sendRedirect(contextPath+"/login.html");
        		else{
        			for (String str : pos) {
        				//学生只能进入与学生有关的页面
						if(str.equals("学生")){
							if(requestPath.contains("/s/") || requestPath.contains("/student/")){
								//班委页面不可进入
								if(requestPath.contains("/s/cc/")) canForward = false;
								else canForward = true;
							}else canForward = false;
						}
						if(str.endsWith("委员") || str.equals("团支书") || str.equals("班长")){
							if(requestPath.contains("/s/") || requestPath.contains("/student/")
									|| requestPath.contains("/s/cc/") || requestPath.contains("/cadre/")){
								//TODO 具体页面与权限
								
								
								canForward = true;
							}else canForward = false;
						}
						if(str.equals("专业教师")){
							if(requestPath.contains("/t/") || requestPath.contains("/teacher/")){
								canForward = true;
							}else canForward = false;
						}
						if(str.equals("辅导员")){
							if(requestPath.contains("/c/") || requestPath.contains("/counselor/")){
								canForward = true;
							}else canForward = false;
						}
					}
        		}
        		//允许遍进入，不然进入登录页面
        		if(canForward)chain.doFilter(request, response);
        		else response.sendRedirect(contextPath+"/login.html");
        	}
        }
	}

	@Override
	public void init(FilterConfig config) throws ServletException {
		//将允许访问的网页路径放入集合中，实际开发中应该是从数据库中取允许访问的路径
//        loginPaths.add("/login.html");
        loginPaths.add("/login.html");
        loginPaths.add("/tchlogin.html");
        loginPaths.add("/codeImg");
        loginPaths.add("/main/checkName.do");
        loginPaths.add("/main/login.do");
	}
}
